//---------------------------------------------------------------------------
//
// Name:        htmlFrame.cpp
// Author:      gurkesaft
// Created:     8/1/2007 9:24:28 AM
// Description: htmlFrame class implementation
//
//---------------------------------------------------------------------------

#include "htmlFrame.h"

//Do not add custom headers between
//Header Include Start and Header Include End
//wxDev-C++ designer will remove them
////Header Include Start
////Header Include End

//----------------------------------------------------------------------------
// htmlFrame
//----------------------------------------------------------------------------
//Add Custom Events only in the appropriate block.
//Code added in other places will be removed by wxDev-C++
////Event Table Start
BEGIN_EVENT_TABLE(htmlFrame,wxFrame)
	////Manual Code Start
	EVT_HTML_LINK_CLICKED(ID_HTMLMAIN, htmlFrame::LinkClicked)
	////Manual Code End

	EVT_CLOSE(htmlFrame::OnClose)
	EVT_SET_FOCUS(htmlFrame::htmlFrameSetFocus)
END_EVENT_TABLE()
////Event Table End

htmlFrame::htmlFrame(RSSFilterFrame *parent, wxWindowID id, const wxString &title, const wxPoint &position, const wxSize& size, long style)
: wxFrame(parent, id, title, position, size, style)
{
	this->parent = parent;
    CreateGUIControls();
}

htmlFrame::~htmlFrame()
{
}

void htmlFrame::CreateGUIControls()
{
	//Do not add custom code between
	//GUI Items Creation Start and GUI Items Creation End
	//wxDev-C++ designer will remove them.
	//Add the custom code before or after the blocks
	////GUI Items Creation Start

	SetTitle(wxT("Filtered Article List"));
	SetIcon(wxNullIcon);
	SetSize(8,8,700,500);
	Center();


	htmlMain = new wxHtmlWindow(this, ID_HTMLMAIN, wxPoint(0,0), wxSize(700,500));
	//htmlMain->SetFont(wxFont(8, wxSWISS, wxNORMAL,wxNORMAL, false, wxT("Tahoma")));
	////GUI Items Creation End

	int sizes[7];
	for(int n=0; n<7; n++) sizes[n] = 0;
	//htmlMain->SetFonts(wxT("Times"), wxT("Courier"), sizes);
}

void htmlFrame::OnClose(wxCloseEvent& event)
{
	Hide();
	parent->master.Save(wxT("User/master.txt"));
}

void htmlFrame::SetPage(wxString &s)
{
    htmlMain->SetPage(s);
}
void htmlFrame::LinkClicked(wxHtmlLinkEvent &link_event)
{
    wxString href;
    int n;

    href = link_event.GetLinkInfo().GetHref();

    if(href == wxT("exit"))
    {
        parent->Quit();
        return;
    }

    if(href == wxT("configure"))
    {
        parent->Show();
        parent->Raise();
        return;
    }

    if(href == wxT("fetch"))
    {
        parent->Fetch();
        return;
    }

    if(href == wxT("undo"))
    {
        // pick the href off the undo list and disable it
        // in the master list
        // (we have to do it this way because the master list changes!)
        href=parent->undo.Last();
        n = parent->master.Enable(href.Strip());

        // pop this undo off the end of the undo list
        parent->undo.RemoveAt(parent->undo.GetCount()-1, 1);

        // Now, just in case we changed the filter criteria, do a full filter/show
        parent->FilterAndShow();
        return;
    }
    if(href == wxT("recall"))
    {
        wxCommandEvent e;
        parent->buttonRecallClick(e);
        return;
    }

    // launch the browser unless it's a "disable" link
    if(href.Find(wxT("disable:")) != 0)
    {
       wxLaunchDefaultBrowser(href);
       return;
    }

    // If we got here, we're removing the article

    // remove the "disable:" tag
    href.Replace(wxT("disable:"), wxT(""));

    // disable it in the master list
    parent->master.Disable(href.Strip());

    // add this action to the undo list
    parent->undo.Add(href);

    // Remove this article from the filtered list
    parent->filtered.Remove(href);
    parent->ShowFiltered();

    if (parent->filtered.GetCount() == 0)
    {
       Hide();
       parent->master.Save(wxT("User/master.txt"));
    }
}

void htmlFrame::htmlFrameSetFocus(wxFocusEvent& event)
{
	parent->FilterAndShow();
}
